import Vue from 'vue'
import VueRouter from 'vue-router'
// 路由懒加载
const Login = () => import( /* webpackChunkName:"login_home_welcome" */ '../components/login.vue')
// import Login from '../components/login.vue'
const Home = () => import( /* webpackChunkName:"login_home_welcome" */ '../components/home.vue')
// import Home from '../components/home.vue'
const Welcome = () => import( /* webpackChunkName:"login_home_welcome" */ '../components/welcome.vue')
// import Welcome from '../components/welcome.vue'


const Users = () => import( /* webpackChunkName:"Users_Rights_Roles" */ '../components/user/users.vue')
// import Users from '../components/user/users.vue'
const Rights = () => import( /* webpackChunkName:"Users_Rights_Roles" */ '../components/power/rights.vue')
// import Rights from '../components/power/rights.vue'
const Roles = () => import( /* webpackChunkName:"Users_Rights_Roles" */ '../components/power/roles.vue')
// import Roles from '../components/power/roles.vue'


const Cate = () => import( /* webpackChunkName:"Cate_Params" */ '../components/goods/categories.vue')
// import Cate from '../components/goods/categories.vue'
const Params = () => import( /* webpackChunkName:"Cate_Params" */ '../components/goods/params.vue')
// import Params from '../components/goods/params.vue'


const GoodsList = () => import( /* webpackChunkName:"GoodsList_GoodsAdd" */ '../components/goods/list.vue')
// import GoodsList from '../components/goods/list.vue'
const GoodsAdd = () => import( /* webpackChunkName:"GoodsList_GoodsAdd" */ '../components/goods/add.vue')
// import GoodsAdd from '../components/goods/add.vue'


const Order = () => import( /* webpackChunkName:"Order_Report" */ '../components/order/order.vue')
// import Order from '../components/order/order.vue'
const Report = () => import( /* webpackChunkName:"Order_Report" */ '../components/goods/add.vue')
// import Report from '../components/report/report.vue'

Vue.use(VueRouter)
const routes = [{
    path: '/',
    redirect: '/Login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: Welcome
    }, {
      path: '/users',
      component: Users
    }, {
      path: '/rights',
      component: Rights
    }, {
      path: '/roles',
      component: Roles
    }, {
      path: '/categories',
      component: Cate
    }, {
      path: '/params',
      component: Params
    }, {
      path: '/goods',
      component: GoodsList
    }, {
      path: '/goods/add',
      component: GoodsAdd
    }, {
      path: '/orders',
      component: Order
    }, {
      path: '/reports',
      component: Report
    }]
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
